$(document).ready(function(){

    $('#tag-num1').on('click', function () {
        $("#tag-num11").empty();
        var opt=$("#tag-num1 option:selected");  //获取选中的option
        var tag = opt.text();   //拿到选中项的文本,比如选中上海，获取的值为“上海”
        var data = {
            type: tag
        }
        $.ajax({
            url:'http://8.130.103.251:8080/good/getBrandByType',	//这是后端接口的url
            dataType: "json",   //返回格式为json
            ContentType: 'application/json;charset=utf-8', //传输格式
            data:data,
            type: "GET",
            success:function (req) {
                console.log(req);
    
                for(var l = 0; l < req.length; l++)
                {
                    var opt = '<option value="' + l + '">' + req[l] + '</option>';
                    $("#tag-num11").append(opt);
                }
    
            },
        });
    });
    var val = document.getElementById("pur-num1");
    $("#yes1").click(function(){
        val.removeAttribute("disabled");
        $("#yes1").attr("checked", "checked");
    });
    $("#no1").click(function(){
        val.setAttribute("disabled", "true");
        $("#yes1").removeAttr("checked");
        $("#pur-num1").val("");
    });


    $(".img-sel2").hide();
    $(".font1").hide();

    $(".btn-change").click(function(){

        $(".img-sel2").show();
        $(".font1").show();

        // var val = document.getElementById("name-val1");
        // val.removeAttribute("disabled");
        // val.setAttribute("style", "border: 1px solid black");
        $("#name-val1").removeAttr("disabled");
        $("#name-val1").css("border", "1px solid black");

        $("#des-txt1").removeAttr("disabled");
        $("#des-txt1").css("border", "1px solid black");

        $("#rent-num1").removeAttr("disabled");
        $("#rent-num1").css("border", "1px solid black");

        $("#rent-num0").removeAttr("disabled");
        $("#rent-num0").css("border", "1px solid black");

        $("#num-num1").removeAttr("disabled");
        $("#num-num1").css("border", "1px solid black");

        $("#pur-num1").removeAttr("disabled");
        $("#pur-num1").css("border", "1px solid black");

        $("#tag-num1").removeAttr("disabled");
        $("#tag-num1").css("border", "1px solid black");

        $("#tag-num11").removeAttr("disabled");
        $("#tag-num11").css("border", "1px solid black");

        $("#yes1").removeAttr("disabled");
        $("#no1").removeAttr("disabled");

        // var btn = document.getElementById("btn-c");
        // btn.innerText = "保存修改";
        // $("#btn-c").addClass("btn-save");
        // $("#btn-c").removeClass("btn-change");
    })

    $(".btn-save").click(function(){
        $(".img-sel2").hide();
        $(".font1").hide();

        $("#name-val1").attr("disabled", "true");
        $("#name-val1").css("border", "1px solid rgb(170,170,170)");

        $("#des-txt1").attr("disabled", "true");
        $("#des-txt1").css("border", "1px solid rgb(170,170,170)");

        $("#rent-num1").attr("disabled", "true");
        $("#rent-num1").css("border", "1px solid rgb(170,170,170)");

        $("#rent-num0").attr("disabled", "true");
        $("#rent-num0").css("border", "1px solid rgb(170,170,170)");

        $("#num-num1").attr("disabled", "true");
        $("#num-num1").css("border", "1px solid rgb(170,170,170)");

        $("#pur-num1").attr("disabled", "true");
        $("#pur-num1").css("border", "1px solid rgb(170,170,170)");

        $("#tag-num1").attr("disabled", "true");
        $("#tag-num1").css("border", "1px solid rgb(170,170,170)");

        $("#tag-num11").attr("disabled", "true");
        $("#tag-num11").css("border", "1px solid rgb(170,170,170)");


        $("#yes1").attr("disabled", "true");
        $("#no1").attr("disabled", "true");

        var price;
        if($("#yes1").attr('checked')){
            isbuy = 1;
            price = $("#pur-num1").val();
        }
        else{
            isbuy = 0;
            price = "";
        }
        console.log(isbuy);

        var id = document.getElementById("id1").innerText;
        var name = $("#name-val1").val();
        var num = $("#num-num1").val();
        var dayprice = $("#rent-num0").val();
        var monthprice = $("#rent-num1").val();
        // var price = $("#pur-num1").val();
        var opt=$("#tag-num1 option:selected");  //获取选中的option
        var tag = opt.text();   //拿到选中项的文本,比如选中上海，获取的值为“上海”

        var opt1=$("#tag-num11 option:selected");  
        var tag1 = opt1.text();   


        var info = $("#des-txt1").val();
        var uid = $("#userid").text();

        var files1 = $('#f4').prop('files');
        var files2 = $('#f5').prop('files');
        var files3 = $('#f6').prop('files');
		 var data = new FormData();
         
         data.append('file1', files1[0]);
         data.append('file2', files2[0]);
         data.append('file3', files3[0]);
        //  data.append('file3', files3[0]);
         data.append('goodid',id);
		 data.append('goodName',name);
         data.append('goodType',tag);
         data.append('goodBrand',tag1);
         data.append('goodPrice',price);
         data.append('dayPrice',dayprice);
         data.append('monthPrice',monthprice);
         data.append('buyStatus',isbuy);
         data.append('goodNum',num);
         data.append('goodInfo',info);
         data.append('seller',uid);
         console.log(data.get("file1"));
		
		$.ajax({
		  url: 'http://8.130.103.251:8080/good/updateGood',
		  type: 'POST',
		  data: data,
		  cache: false, 
		  processData: false,
		  contentType: false,
		  success:function(res){
			console.log(JSON.stringify(res));
		  }
		 });

    })

});